import { createDiscreteApi } from "naive-ui";

export default defineNuxtPlugin((nuxtApp) => {
  const bar = ref(null);

  nuxtApp.hook("app:created", (e) => {
    // console.log("app:created");
    if (!bar.value) {
      const { loadingBar } = createDiscreteApi(["loadingBar"]);
      bar.value = loadingBar;
    }
  });
  nuxtApp.hook("page:start", (e) => {
    // console.log("page:start");
    bar.value?.start();
  });
  nuxtApp.hook("page:finish", (e) => {
    setTimeout(() => {
      bar.value?.finish();
    }, 150);
    // console.log("page:finish");
  });
  nuxtApp.hook("app:error", (e) => {
    // console.log("app:error");
    if (import.meta.client) {
      setTimeout(() => {
        bar.value?.finish();
      }, 150);
    }
  });
});
